//
//  XJCodeView.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCodeView.h"

@interface XJCodeView ()

@property (nonatomic, copy) NSArray * codeArray;

@end

@implementation XJCodeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [self initWithTime:_time frame:frame];
    if (self) {
        self.backgroundColor = WHITE_COLOR;
    }
    return self;
}

- (instancetype)initWithTime:(NSInteger)time
                       frame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.time = time;
        [self initSendValidateBtn];
        [self initTimer];
    }
    return self;
    
}

- (NSArray *)codeArray{
    
    if (!_codeArray) {
        _codeArray = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9" ,nil];
    }
    return _codeArray;
}

- (UIButton *)codeBtn{
    
    if (!_codeBtn) {
        UIButton * codeBtn = [UIButton new];
        self.codeBtn = codeBtn;
        [self addSubview:codeBtn];
    }
    return _codeBtn;
}

- (void)initSendValidateBtn{
        self.codeBtn.frame=CGRectMake(0, 0, 79, 30);
        [self.codeBtn setTitle:@"获取" forState:UIControlStateNormal];
        [self.codeBtn addTarget:self action:@selector(clickGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.codeBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
        self.codeBtn.backgroundColor = MAIN_COLOR;
        self.codeBtn.userInteractionEnabled = YES;
        self.codeBtn.layer.cornerRadius = 15;
        self.codeBtn.titleLabel.font=XJFONTSET_R(15);
        self.codeBtn.titleLabel.adjustsFontSizeToFitWidth=YES;//button字体自适应宽度
}

- (void)initTimer{
    _countTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeBegin) userInfo:nil repeats:YES];//启动计时器后每一秒调用一次timeBegin方法
    [[NSRunLoop mainRunLoop]addTimer:_countTimer forMode:NSDefaultRunLoopMode];
    [_countTimer setFireDate:[NSDate distantFuture]];
}
- (void)timeBegin{
    self.time --;
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%ldS",self.time] forState:UIControlStateNormal];
    self.codeBtn.backgroundColor = XJCOLOR_RGB(238, 238, 238);
    [self.codeBtn setTitleColor:XJCOLOR_RGB(185, 194, 197) forState:UIControlStateNormal];
    //   XXLog(@"%ld",self.time);
    if (self.time==0) {
        [_countTimer setFireDate:[NSDate distantFuture]];
        [self.codeBtn setTitle:@"获取" forState:UIControlStateNormal];
        [self.codeBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
        self.codeBtn.backgroundColor = MAIN_COLOR;
        self.codeBtn.userInteractionEnabled = YES;
        self.time = 60;
        
    }
}

#pragma mark - ********生成验证码
-(NSString *)creatChangeNum{
    NSMutableString *getStr=[[NSMutableString alloc]initWithCapacity:5];
    NSMutableString *userChangeStr=[[NSMutableString alloc]initWithCapacity:6];
    for (int x=0; x<4; x++) {
        NSInteger index=arc4random()%(self.codeArray.count-1);
        getStr=[self.codeArray objectAtIndex:index];
        userChangeStr=(NSMutableString *)[userChangeStr stringByAppendingString:getStr];
    }
    [_countTimer setFireDate:[NSDate date]];
    self.codeBtn.userInteractionEnabled = NO;
    return userChangeStr;
}
- (void)clickGetCodeBtn:(UIButton *)sender{
    
    if (_delegate &&[_delegate respondsToSelector:@selector(getCheckCode:)]) {
        [_delegate getCheckCode:[self creatChangeNum]];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
